package com.tdw.eurekaclient.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import com.tdw.eurekaclient.dto.UserInfo;

public class UserCacheCommand extends HystrixCommand<UserInfo> {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserCacheCommand.class);

    private static final HystrixCommandKey GETTER_KEY= HystrixCommandKey.Factory.asKey("CommandKey");
    private RestTemplate restTemplate;
    private String username;

    public UserCacheCommand(RestTemplate restTemplate, String username) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userCacheCommand")).andCommandKey(GETTER_KEY));
        this.restTemplate = restTemplate;
        this.username = username;
    }

    @Override
    protected UserInfo run() throws Exception {
        LOGGER.info("thread:" + Thread.currentThread().getName());
        return restTemplate.getForObject("http://users-service/user/name/{username}", UserInfo.class, username);
    }

    @Override
    protected UserInfo getFallback() {
    	UserInfo user = new UserInfo();
        user.setId(1l);
        user.setName("调用失败");
        return user;
    }

    @Override
    protected String getCacheKey() {
        return username;
    }

    public static void flushCache(String username){
        HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(username);
    }
}